package com.movitech.mobile.cz.base.purchase.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.purchase.entity.WFlowFormQaCountSign;
import com.movitech.mobile.cz.base.purchase.mapper.WFlowFormQaCountSignMapper;
import com.movitech.mobile.cz.base.purchase.service.WFlowFormQaCountSignService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 流程表单-答疑补遗-答疑会签 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-03-15
 */
@Service
public class WFlowFormQaCountSignServiceImpl extends ServiceImpl<WFlowFormQaCountSignMapper, WFlowFormQaCountSign> implements WFlowFormQaCountSignService {

    @Override
    public Integer selectMaxTimesByFormIdAndRelate(String formId, String relateId, Integer type) {
        WFlowFormQaCountSign wFlowFormQaCountSign = new WFlowFormQaCountSign();
        wFlowFormQaCountSign.setRelatedId(relateId);
        wFlowFormQaCountSign.setFlowFormId(formId);
        wFlowFormQaCountSign.setCountSignType(type);
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.setEntity(wFlowFormQaCountSign);
        entityWrapper.orderBy("create_date",false);
        wFlowFormQaCountSign = selectOne(entityWrapper);
        if(wFlowFormQaCountSign==null)
             return 0;
        else
            return wFlowFormQaCountSign.getCountSignTimes();
    }

    @Override
    public void updateStatusById(String subRelatedId, int i) {
        WFlowFormQaCountSign wFlowFormQaCountSign = new WFlowFormQaCountSign();
        wFlowFormQaCountSign.setPassOrNot(i);
        wFlowFormQaCountSign.setId(subRelatedId);
        updateById(wFlowFormQaCountSign);
    }

    @Override
    public void handleNotGo(String formId, String relateId, String userId) {
        WFlowFormQaCountSign  wFlowFormQaCountSign = new WFlowFormQaCountSign();
        wFlowFormQaCountSign.setDelFlag(CommonConstants.DEL_FLAG_DELETE);
        update(wFlowFormQaCountSign,new EntityWrapper().eq("flow_form_id",formId).
                eq("related_id",relateId).eq("pass_or_not",null).notIn("user_id",userId));
    }

    @Override
    public List<WFlowFormQaCountSign> selectByFormIdAndRelateId(String formId, String relateId,Integer type,Integer times) {
        EntityWrapper entityWrapper = new EntityWrapper();
        WFlowFormQaCountSign wFlowFormQaCountSign = new WFlowFormQaCountSign();
        wFlowFormQaCountSign.setFlowFormId(formId);
        wFlowFormQaCountSign.setRelatedId(relateId);
        wFlowFormQaCountSign.setCountSignType(type);
        wFlowFormQaCountSign.setCountSignTimes(times);
        wFlowFormQaCountSign.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
        entityWrapper.setEntity(wFlowFormQaCountSign);
        entityWrapper.orderBy("count_sign_times",false).orderBy("update_date",false);
        return selectList(entityWrapper);
    }

    @Override
    public List<WFlowFormQaCountSign> selectByEntity(WFlowFormQaCountSign entity) {
        EntityWrapper entityWrapper = new EntityWrapper();
        entity.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
        entityWrapper.setEntity(entity);
        entityWrapper.orderBy("count_sign_times",false).orderBy("update_date",false);
        return selectList(entityWrapper);
    }

    @Override
    public void updateStatusByEntity( WFlowFormQaCountSign where) {
        EntityWrapper entityWrapper = new EntityWrapper();
        Integer status = where.getPassOrNot();
        where.setPassOrNot(null);
        entityWrapper.setEntity(where);

        WFlowFormQaCountSign wFlowFormQaCountSign = new WFlowFormQaCountSign();
        wFlowFormQaCountSign.setPassOrNot(status);
        update(wFlowFormQaCountSign,entityWrapper);

    }
}
